#!/usr/bin/env bash

export DEBIAN_FRONTEND=noninteractive
ufw disable
apt-get -q -y purge php5-fpm
apt-get -q -y purge apache2
apt-get -q -y autoremove
cp /vagrant/setup/* /home/vagrant/
SITE_PATH='/var/www/magento'
echo "deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty main restricted
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty-updates main restricted
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty-updates universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty-updates multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty-security main restricted
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty-security universe
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ zesty-security multiverse
" > /etc/apt/sources.list

apt-get -q -y install python-software-properties
add-apt-repository -y ppa:ondrej/php
add-apt-repository -y ppa:nginx/stable
apt-get -q -y update
apt-get -q -y install unzip

# Install MySQL
DEBIAN_FRONTEND=noninteractive apt-get -q -y install mysql-server mysql-client

# Create Database instance
mysql -u root -e "use mysql;update user set host = '%';"
mysql -u root -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '' WITH GRANT OPTION;"
mysql -u root -e "flush privileges;"
mysql -u root -e "create database magento;"

# Install PHP 7.0
apt-get -q -y install libcurl4-openssl-dev php7.0-cli php7.0-common php7.0 php7.0-mysql php7.0-fpm php7.0-gd php7.0-mysql php7.0-bz2 php7.0-dev php7.0-xml php7.0-mcrypt php7.0-intl php7.0-mbstring php7.0-zip php-pear php7.0-soap php7.0-bcmath php7.0-dom php7.0-curl

# nginx
apt-get install -q -y nginx
mkdir "$SITE_PATH"
mv ./Magento*.zip "$SITE_PATH/"
unzip "$SITE_PATH/*.zip"
chmod 0777 -R "$SITE_PATH/"
chown -R www-data:www-data "$SITE_PATH/"
echo "upstream fastcgi_backend {
	server  unix:/var/run/php/php7.0-fpm.sock;
}

server {
	listen 80;
	server_name 192.168.33.10;
	set \$MAGE_ROOT $SITE_PATH;
	set \$MAGE_MODE developer;
	include $SITE_PATH/nginx.conf.sample;
}" > /etc/nginx/sites-available/magento
ln -s /etc/nginx/sites-available/magento /etc/nginx/sites-enabled/

# redis
python /vagrant/scripts/redis_download.py
tar xzf redis-4.0.3.tar.gz
cd /home/vagrant/redis-4.0.3/
/usr/bin/make
sed -i 's/daemonize no/daemonize yes/g' /home/vagrant/redis-4.0.3/redis.conf
mkdir /etc/redis/
cp /home/vagrant/redis-4.0.3/redis.conf /etc/redis/6379.conf
cp /home/vagrant/redis-4.0.3/utils/redis_init_script /etc/init.d/redis
ln -s /home/vagrant/redis-4.0.3/src/redis-server /usr/local/bin/redis-server
ln -s /home/vagrant/redis-4.0.3/src/redis-cli /usr/local/bin/redis-cli
service redis start

# auto start
echo "#!/bin/sh -e
/etc/init.d/redis start
/etc/init.d/nginx start
exit 0" > /etc/rc.local
service php7.0-fpm restart
service nginx start

# Post Up Message
echo "安装完成"
echo "访问 http://192.168.33.10/setup/ 进行安装"